package br.uvv.sao.dao;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.uvv.sao.domain.Prova;
import br.uvv.sao.enums.SituUsuario;
import br.uvv.sao.util.DAOException;

@SuppressWarnings("unchecked")
public class ProvaDAO extends DAO {
	
	private static final long serialVersionUID = 3427794905504675390L;

	public void salvar(Prova provaInstance) throws DAOException {
		save(provaInstance, provaInstance.getId());
	}
	
	public void excluir(Prova provaInstance) throws DAOException {
		delete(Prova.class, provaInstance.getId());
	}

	public Prova selecionar(Long id) throws DAOException {
		return (Prova) get(Prova.class, id);
	}
	
	public List<Prova> listar() throws DAOException {
		Criterion[] filtros = {
			Restrictions.eq("situacao", SituUsuario.Ativo)
		};
		return list(Prova.class, null, Order.asc("id"), filtros);
	}

	public List<Prova> listar(int first, int pageSize, String sortField,
			boolean sortOrder, String varBuscaCodigo, String varBuscaDescricao) throws DAOException {
		Criterion[] filtros = {
			Restrictions.ilike("codigo", "%" + varBuscaCodigo +  "%"),
			Restrictions.ilike("descricao", "%" + varBuscaDescricao +  "%")
		};
		return listPaginator(first, pageSize, sortField, sortOrder, Prova.class, null, filtros);
	}
	
	public Integer rowCount() throws DAOException {
		return rowCount(Prova.class);
	}

	public List<Prova> listarProvasEmOferta(Long idOferta) throws DAOException {
		Criterion[] filtros = {
				Restrictions.eq("aplicadaEm.id", idOferta)
		};
		return list(Prova.class, null, Order.desc("dataHoraFim"), filtros);
	}
}